package priv.ln.team.service;

import priv.ln.team.domain.Architect;
import priv.ln.team.domain.Designer;
import priv.ln.team.domain.Employee;
import priv.ln.team.domain.Programmer;

/**
 * ClassName: TeamService
 * Package: priv.ln.team.service
 * Description:
 * 关于开发团队成员的管理：添加、删除等
 *
 * @Author 辣辣
 * @Create 2025/4/17 17:36
 * @Version 1.0
 */
public class TeamService {
    private static int counter = 1;//静态变量，用来为开发团队新增成员自动生成团队中的唯一ID，即memberId
    private static final int MAX_MEMBER = 5;
    private Programmer[] team = new Programmer[MAX_MEMBER];//当前团队中的各成员对象
    private int total = 0;//团队成员的实际人数

    public Programmer[] getTeam() {
        Programmer[] realTeam = new Programmer[total];
        for (int i = 0; i < total; i++) {
            realTeam[i] = team[i];
        }
        return realTeam;
    }

    public void addMember(Employee e) throws TeamException {
        if (total == team.length) {
            throw new TeamException("成员已满，无法添加");
        }

        if (!(e instanceof Programmer))
            throw new TeamException("该成员不是开发人员，无法添加");

        Programmer p = (Programmer) e;
        switch (p.getStatus()) {
            case BUSY:
                throw new TeamException("该员工已是某团队成员");
            case VOCATION:
                throw new TeamException("该员正在休假，无法添加");
        }

        int pcount = 0;
        int dcount = 0;
        int acount = 0;
        for (int i = 0; i < total; i++) {
            if (team[i].equals(e)) {
                throw new TeamException("该员工已在本开发团队中");
            }
            if (team[i] instanceof Architect) {
                acount++;
            } else if (team[i] instanceof Designer) {
                dcount++;
            } else {
                pcount++;
            }
        }
        if (p instanceof Architect && acount >= 1) {
            throw new TeamException("团队中至多只能有一名架构师");
        }
        if (p instanceof Designer && dcount >= 2) {
            throw new TeamException("团队中至多只能有两名设计师");
        }
        if (pcount >= 3) {
            throw new TeamException("团队中至多只能有三名程序员");
        }


        team[total++] = (Programmer) e;
        ((Programmer) e).setStatus(Status.BUSY);
        ((Programmer) e).setMemberId(counter++);
        System.out.println("添加成功");
    }

    public void removeMember(int memberId) throws TeamException {
        boolean flag = false;
        int index = 0;
        for (int i = 0; i < total; i++) {
            if (team[i].getMemberId() == memberId) {
                index = i;
                flag = true;
            }
        }
        if (!flag) {
            throw new TeamException("找不到指定memberId的员工，删除失败");
        }
        team[index].setStatus(Status.FREE);
        for (int i = index; i < total - 1; i++) {
            team[i] = team[i + 1];
        }
        team[--total] = null;
        System.out.println("删除成功");
    }

    public static int getCounter() {
        return counter;
    }

    public static void setCounter(int counter) {
        TeamService.counter = counter;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}
